package com.hxzy.controller.admin.dictdata;

import com.github.pagehelper.PageInfo;
import com.hxzy.common.constans.RedisConstans;
import com.hxzy.common.enums.AckCode;
import com.hxzy.common.vo.req.PageReq;
import com.hxzy.common.vo.resp.PageResp;
import com.hxzy.common.vo.resp.R;
import com.hxzy.controller.admin.dictdata.req.DictDataAddReq;
import com.hxzy.controller.admin.dictdata.req.DictDataEditReq;
import com.hxzy.controller.admin.dictdata.req.DictDataReq;
import com.hxzy.controller.admin.dictdata.resp.DictDataResp;
import com.hxzy.controller.admin.mapstruct.DictDataConvert;
import com.hxzy.entity.Employee;
import com.hxzy.entity.SysDictData;
import com.hxzy.entity.SysDictType;
import com.hxzy.service.SysDictDataService;
import com.hxzy.util.DictCacheUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @Desc 数据字典明细
 * @Author: admin
 * @Date: 2023/07/03/16:01
 */
@Api(tags = "管理后台-数据字典明细")
@RestController
@RequestMapping(value = "/api/dictdata")
public class DictDataController {

    @Autowired
    private SysDictDataService sysDictDataService;

    /**
     * redis缓存字典
     */
    @Autowired
    private DictCacheUtil dictCacheUtil;


    @ApiOperation(value = "根据dict_type查询redis缓存中的值")
    @GetMapping(value = "/bynames")
    public R<Map<String, List<DictDataResp>>>  bynames(String[] dicts){
        if(Objects.isNull(dicts) || dicts.length==0){
            return R.build(AckCode.SYSTEM_PARAM_FAIL);
        }

        Map<String,List<DictDataResp>> dataMap=new HashMap<>();
        for(String key : dicts){
            List<SysDictData> byDictType = this.dictCacheUtil.findByDictType(key);
            //有缓存
            if(Objects.nonNull(byDictType)){
                dataMap.put(key, DictDataConvert.INSTANCE.convert(byDictType));
            }
        }
        return R.okHasData(dataMap);
    }


    @ApiOperation(value = "分页查询")
    @GetMapping(value = "/data")
    public R<PageResp<DictDataResp>> search(@Valid DictDataReq req){
        PageInfo<SysDictData> pageInfo=this.sysDictDataService.search(req);

        PageResp<DictDataResp> resp=new PageResp<>();
        resp.setTotal(pageInfo.getTotal());
        resp.setRows(DictDataConvert.INSTANCE.convert(pageInfo.getList()));
        return R.okHasData(resp);
    }

    @ApiOperation(value = "新增")
    @PostMapping
    public R add(@RequestBody @Valid DictDataAddReq req){
       SysDictData entity= DictDataConvert.INSTANCE.convert(req);
       boolean save = this.sysDictDataService.save(entity);
       //更新缓存
        this.dictCacheUtil.refreshCacheByDictType(req.getDictType());
       return save? R.ok(): R.build(AckCode.DATABASE_ERROR);
    }

    @ApiOperation(value = "根据主键查询")
    @GetMapping(value = "/{id}")
    public R<DictDataResp> findById(Long id){
        if(Objects.isNull(id) || id.intValue()<=0){
            return R.build(AckCode.SYSTEM_PARAM_FAIL);
        }

        SysDictData db = this.sysDictDataService.getById(id);
        if(Objects.isNull(db)){
            return R.build(AckCode.DATA_NOT_FOUND);
        }

        DictDataResp resp=DictDataConvert.INSTANCE.convert(db);
        return R.okHasData(resp);
    }

    @ApiOperation(value = "修改")
    @PutMapping
    public R update(@RequestBody @Valid DictDataEditReq req){
        if(Objects.isNull(req.getId()) || req.getId().intValue()<=0){
            return R.build(AckCode.SYSTEM_PARAM_FAIL);
        }

        SysDictData db = this.sysDictDataService.getById(req.getId());
        if(Objects.isNull(db)){
            return R.build(AckCode.DATA_NOT_FOUND);
        }

        SysDictData entity=DictDataConvert.INSTANCE.convert(req);
        boolean save = this.sysDictDataService.updateById(entity);

        //更新缓存
        this.dictCacheUtil.refreshCacheByDictType(req.getDictType());

        return save? R.ok(): R.build(AckCode.DATABASE_ERROR);
    }

    @ApiOperation(value = "逻辑删除")
    @DeleteMapping
    public R delete(Long[] ids){
        if(Objects.isNull(ids) || ids.length==0){
            return R.build(AckCode.SYSTEM_PARAM_FAIL);
        }

        //1、查询禁用操作的dictType值
        SysDictData db = this.sysDictDataService.getById(ids[0]);

        //2、更新数据库的缓存
        boolean result=this.sysDictDataService.delete(ids);

        //3、更新redis的缓存
        this.dictCacheUtil.refreshCacheByDictType(db.getDictType());

        return result? R.ok(): R.build(AckCode.DATABASE_ERROR);
    }
}
